using System.Collections.Generic;
using System.Linq;
using GorillaEngine.Core.Components;
using GorillaEngine.Core.Engine;
using GorillaEngine.Dependency;
using GorillaEngine.ICore;
using GorillaEngine.Particles.Effects;
using GorillaEngine.Particles.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Affenzirkus
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Affenzirkus : InjectedGame
    {
        GraphicsDeviceManager _graphics;
        SpriteBatch _spriteBatch;
        private readonly ContentLoader _contentLoader;
        private readonly IParticleEngine _particleEngine;
        private readonly IRenderEngine _renderEngine;
        private readonly IPhysicsEngine _physicsEngine;
        private readonly IRandomizer _randomizer;

        private List<Particle> _particles;

        public Affenzirkus() {
            _randomizer = new Randomizer(1337);
            _particleEngine = new ParticleEngine();
            _renderEngine = new SimpleRenderEngine();
            _physicsEngine = new PhysicsEngine();
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            _contentLoader = new ContentLoader(Content);
            _particles = new List<Particle>();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // CreateEffect a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime) {
            var mousstate = Mouse.GetState();

            if (mousstate.LeftButton == ButtonState.Pressed){

                if (_particles.OfType<IParticle>().Select(x => x).Count() <= 10000)
                    _particles.AddRange(ExplosionEffect.CreateEffect(_contentLoader.Get("Textures\\circle"),new Vector2(mousstate.X, mousstate.Y), _randomizer));
            }

            var particles = _particles.OfType<IParticle>().Select(x => x).ToList();
            var physicsables = _particles.OfType<IPhysicsObject>().Select(x => x).ToList();

            _particles = _particleEngine.UpdateParticles(gameTime, particles);
            _physicsEngine.AddPhysicElements(physicsables);
            _physicsEngine.Apply();

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            var position = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
            _renderEngine.AddAdditionalRenderActions(x => x.Draw(_contentLoader.Get("Textures\\circle"), position, Color.White));

            var drawables = _particles.OfType<IExtendedDrawable>().Select(x => x).ToList();

            _renderEngine.AddObjectsForRendering(drawables);
            _renderEngine.Render(_spriteBatch,gameTime);
            base.Draw(gameTime);
        }
    }
}
